package com.yj.consumer.dynamicDataSource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.stereotype.Component;

public class DynamicDataSource extends AbstractRoutingDataSource {

    private final static ThreadLocal<String> dataSourceContext = new ThreadLocal<>();


    @Override
    protected Object determineCurrentLookupKey() {
        return dataSourceContext.get();
    }


    public static void setDataSourceContext(String dbName){
        dataSourceContext.set(dbName);
    }

    public static String getDataSourceName(){
        return dataSourceContext.get();
    }

    public static void remove(){
        dataSourceContext.remove();
    }

}
